package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import org.impl.UserDAOProxy;
import org.vo.UserVo;
public class UpdateUser {
	  private  JFrame frame=new JFrame("修改");
	  private  JTable table;
	  private Object[] column={"ID","姓名","性别","密码"};
	  private 	Object[][] row=new Object[1][4];
	  private	DefaultTableModel model=null;
	  private 	JScrollPane scrollpane =null;
	  private JComboBox sex=new JComboBox();
	  private   JButton but1=new JButton("确定");
	  private   JButton but2=new JButton("取消");
	  private	DefaultTableModel bigModel=null;
	  private  JTable bigTable;
	  private   Object[] bigColumn={"ID","姓名","性别","密码"};
	  private  Object[][] bigRow=new Object[30][4];
	  
		public UpdateUser() {
			// TODO Auto-generated constructor stub
			model=new DefaultTableModel(row,column){  
	        
	            public boolean isCellEditable(int row,int column){  //设置ID不能被修改
	            	if(row==0&&column==0)
	                    return false;  
	            	else
	            		return true;
	            }  
	        };  
			table=new JTable(model);
			table.getTableHeader().setReorderingAllowed(false); 
			this.sex.addItem("男");
			this.sex.addItem("女");
			table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(this.sex));
		     but1.setBounds(135, 90, 60, 30);
			 but2.setBounds(225, 90, 60, 30);
			 frame.add(but1);
			frame.add(but2);
			scrollpane=new 	JScrollPane(table);
			frame.setLayout(null);
		    scrollpane.setBounds(3,15,400,50);
			frame.add(scrollpane);
			frame.setSize(420, 180);
			frame.setVisible(true);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setLocationRelativeTo(null);
			but1.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					frame.dispose();
					UserVo user=new UserVo();
					user.setId((String)table.getValueAt(0, 0));
					user.setUserName((String)table.getValueAt(0,1));
					user.setSex((String)table.getValueAt(0, 2));
					user.setPassword((String)table.getValueAt(0,3));
					try {
					  	if(new UserDAOProxy().update(user))       //修改用户信息
					  		{		List<UserVo> user2=null;                
							try {
								user2=new UserDAOProxy().searchAll();    //获取数据库表格内容
							} catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}    
							for(int i=0;i<user2.size();i++){              //设置显示内容
								bigRow[i][0]=user2.get(i).getId();
								bigRow[i][1]=user2.get(i).getUserName();
								bigRow[i][2]=user2.get(i).getSex();
								bigRow[i][3]=user2.get(i).getPassword();
								
							}
							bigModel=new DefaultTableModel(bigRow,bigColumn);
							bigTable.setModel(bigModel);                //对UserTable进行实时更新！！！！！！！！！这样写是否不妥？？？？？？？
					  		}
					
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
			});
			but2.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					frame.dispose();
				}
				
			});
		}
	   public UpdateUser(String sId,String sName,String sSex,String sPassword){
		  this();
		  model.setValueAt(sId, 0, 0); 
		  model.setValueAt(sName, 0, 1);
		  model.setValueAt(sSex, 0, 2);
		  model.setValueAt(sPassword, 0, 3);
		   
	   }
		public UpdateUser(JTable bigTable, String sId,String sName,String sSex,String sPassword) {
		// TODO Auto-generated constructor stub
			this(sId,sName,sSex,sPassword);
			this.bigTable=bigTable;            //接收UserTable 对象实现UserTable的JTable更新
			
	}
		public static void main(String[] args) {
			// TODO Auto-generated method stub
	        new UpdateUser();
		}

	}
